// pages/userCenter/merIdentify.js
var rsa = require('../../utils/rsa.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    src:'',
    bsrc:'',
    srcflag:false,
    bsrcflag:false,
    openbank:'',  
    cardnum:'',
    uname:'',
    tel:'',
    address:'',
    useflag:false,
    known:true,
    xiyi_text:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.ctx = wx.createCameraContext()
  },
  sch:function(){
    this.setData({
      useflag:false,
    })
  },
  //使用协议
  showmodele:function(){
    // var xiy = this.data.xiyi_text;
    // if(xiy ==''){
    //   this.requireNode();
    // }
    // this.setData({
    //   useflag:true,
    // })
    this.requireNode();
  },
  //请求协议
  requireNode:function(){
   wx.downloadFile({
     url:'https://yklmt.etonepay.com/img_local/aa.docx',
      success: function (res) {
       // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
       if (res.statusCode === 200) {
         var filePath = res.tempFilePath
         wx.openDocument({
           filePath: filePath,
           success: function (res) {
             console.log('打开文档成功')
           }
         })
       }
     }

   })
    // rsa.httpPost('basic_agreement', {}, (err, res) => {
    //   if (err) {
    //     wx.showToast({
    //       title: err,
    //       icon: 'none',
    //       duration: 4000,
    //     })
    //     return;
    //   }
    //   this.setData({
    //     xiyi_text: res.data.message
    //   })
     
    // })
  },
  //checkbox
  checkboxChange:function(e){
    console.log(e);
  
    const vale = e.detail.value;
    var isknow = true;
    if(vale == '1'){
      isknow = true;
    }else{
      isknow = false;
    }
    this.data.known = isknow;
  
  },
  // 拍照
  getLocalImage: function (e) {
    var that = this;
    const merid = app.globalData.merId;
    const index = e.currentTarget.dataset.info;
    wx.chooseImage({
      count: 1,
      success: function (res) {
        // 这里无论用户是从相册选择还是直接用相机拍摄，拍摄完成后的图片临时路径都会传递进来
        var filePath = res.tempFilePaths[0];
        var fordata = "";
       if(index ==1){
         fordata = 'a';
       }else{
         fordata =  'b' ;
       }
        // 这里顺道展示一下如何将上传上来的文件返回给后端，就是调用wx.uploadFile函数
        wx.uploadFile({
          url: 'https://yklmt.etonepay.com/pos/basicSend/basic_uploadPicture',
          filePath: filePath,
          name: 'file',
          formData: { 'merId': merid, "imgType": fordata},
          success: function (res) {
            var redata = JSON.parse(res.data);

            if (index == 1) {
              var noticie = false;
              if (redata.return_code == '0000') {
                noticie = true
              }else{
                noticie = false
              }
              that.setData({
                src: filePath,
                srcflag:noticie
              })
            } else {
              var noticie = false;
              if (redata.return_code == '0000') {
                noticie = true
              } else {
                noticie = false
              }
              that.setData({
                bsrc: filePath,
                bsrcflag:noticie
              })
            }
          } 
        })
      },
      fail: function (error) {
        console.error("调用本地相册文件时出错")
      },
      complete: function () {

      }
    })
  },
  //提交认证
  uploadAction:function(){
    const isknow = this.data.known;
    if(!isknow)return;
    var mess = '';
    const openbank = this.data.openbank;
    const carnum = this.data.cardnum;
    const name = this.data.uname;
    const tel = this.data.tel;
    const address = this.data.address;
    const val1 = this.data.srcflag;
    const val2 = this.data.bsrcflag;
    if(!openbank){
      mess = "开户行不为空";
    }else if(!carnum){
      mess = "卡号不为空";
    }else if(!name){
      mess = "持卡人姓名必须填写";
    }else if(!tel){
      mess = "绑定手机号必填";
    }else if(!address){
      mess = '营业地址必须填写';
    }else if(!val1){
      mess = '身份证必须上传';
    }else if(!val2){
      mess = '营业执照必须上传';
    }else{
    }
    if(mess){
      wx.showToast({
        title: mess,
        icon:'none',
        duration:3000,
      })
      return;
    }
    const merid = app.globalData.merId;
    const pot = 'basic_perfectBn';
    rsa.httpPost(pot, { "merId": merid, 'bankAccount': carnum, 'merAddress': address, "contactName": name, 'contactPhone': tel,'openAccount':openbank }, (err, res) => {
      if (err) {
        wx.showToast({
          title: err,
          icon: 'none',
          duration: 4000,
        })
        return;
      }
      if (res.return_code == '0000'){
        wx.showModal({
          title: '提示',
          content: '提交成功，等待审核',
          cancelText: '返回',
          success: function (res) {
            if (res.confirm) {
            } else if (res.cancel) {
              wx.navigateBack({
                delta: 1,
              })
            }
          }
        })
      }else{
        wx.showToast({
          title: '提交失败',
          icon:'none',
          duration:3000,
        })
      }
      
     
    })
  },
  openbank:function(e){
    this.data.openbank = e.detail.value;
  },
  cardNumA:function(e){
    this.data.cardnum = e.detail.value;
  },
  userNameA:function(e){
    this.data.uname = e.detail.value;
  },
  telephoneA:function(e){
    this.data.tel = e.detail.value;
  },
  addressA:function(e){
    this.data.address = e.detail.value;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})